package com.list;

public class Test1 {
    //合并两个链
    public static ListNode mergeTwoLists(ListNode l1, ListNode l2){
        ListNode pump = new ListNode(-1),p=pump;
        ListNode p1=l1,p2=l2;
        while (p1!=null&&p2!=null){
            if (p1.val< p2.val){
                p.next=p1;
                p1=p1.next;
            }else {
                p.next=p2;
                p2=p2.next;
            }
            p=p.next;
        }
        if (p1!=null){
            p.next=p1;
        }
        if (p2!=null){
            p.next=p2;
        }
        return pump.next;

    }
    public static ListNode mergeTwoLists2(ListNode l1, ListNode l2){
       if (l1==null){
           return l2;
       }else if (l2==null){
           return l1;
       }else if (l1.val<l2.val){
           l1.next=mergeTwoLists2(l1.next,l2);
           return l1;
       }else {
           l2.next=mergeTwoLists2(l2.next,l1);
           return l2;
       }
    }

}
